<?php

namespace app\common\model;

use app\beautiful\model\PrinterModel;
use app\common\model\account\ShopAccountModel;
use think\facade\Db;


class ProduceModel extends CommonModel{

    protected  $name = 'beautiful_produce';

    protected  $pk   = 'id';


    public static function info($where){
        $info = Db::name('beautiful_produce')->alias('a')
            ->join('beautiful_goods b','a.goods_id=b.goods_id')
            ->join('beautiful_goods_category c','b.category_id=c.category_id')
            ->join('beautiful_produce_depart d','a.depart_id=d.id')
            ->where(['a.is_delete'=>0])
            ->when(!empty($where['shop_id']),function ($query) use ($where){
                $query->where(['a.shop_id'=>$where['shop_id']]);
            })
            ->when(!empty($where['id']),function ($query) use ($where){
                $query->where(['a.id'=>$where['id']]);
            })
            ->when(!empty($where['depart_id']),function ($query) use ($where){
                $query->where(['a.depart_id'=>$where['depart_id']]);
            })
            ->when(!empty($where['category_id']),function ($query) use ($where){
                $query->where(['b.category_id'=>$where['category_id']]);
            })
            ->when(isset($where['goods_id']) && !empty($where['goods_id']),function ($query) use ($where){
                $query->where(['a.goods_id'=>$where['goods_id']]);
            })
            ->field("a.*,b.name as goods_name,c.name as category_name,d.name as depart_name")
            ->find();
        return $info;
    }
    public static function list($where,$order,$page=1,$limit=10,$flash_sale_goods=[],$pin_tuan_goods=[]){
        $query = Db::name('beautiful_produce')->alias('a')
            ->join('beautiful_goods b','a.goods_id=b.goods_id')
            ->join('beautiful_goods_category c','b.category_id=c.category_id')
            ->join('beautiful_produce_depart d','a.depart_id=d.id')
            ->where(['a.is_delete'=>0])
            ->when(!empty($where['shop_id']),function ($query) use ($where){
                $query->where(['a.shop_id'=>$where['shop_id']]);
            })
            ->when(isset($where['is_delete']),function ($query) use ($where){
                $query->where(['d.is_delete'=>$where['is_delete']]);
            })
            ->when(isset($where['depart_id']) && !empty($where['depart_id']),function ($query) use ($where){
                $query->where(['a.depart_id'=>$where['depart_id']]);
            })
            ->when(isset($where['category_id']) && !empty($where['category_id']),function ($query) use ($where){
                $query->where(['b.category_id'=>$where['category_id']]);
            })
            ->when(isset($where['category_ids']) && !empty($where['category_ids']),function ($query) use ($where){
                $query->whereIn('b.category_id',$where['category_ids']);
            })
            ->when(isset($where['goods_id']) && !empty($where['goods_id']),function ($query) use ($where){
                $query->whereIn('a.goods_id',$where['goods_id']);
            })
            ->field("a.*,b.name as goods_name,c.name as category_name,d.name as depart_name,d.print_ids,d.is_effect");
        $count=$query->count();
        $list=$query->order($order)
            ->limit($limit)->page($page)
            ->select()
            ->toArray();
        $prints = [];
        if($list){
            foreach ($list as $k=>$v){
                $print_ids = explode(',',$v['print_ids']);
                if($v['is_effect']==1){
                    for ($i=0;$i<count($print_ids);$i++){
                        $print = PrinterModel::whereIn('printer_id',$print_ids[$i])->find();
                        $prints[$print_ids[$i]]['print'] = $print?$print->toArray():$print;

                        if($flash_sale_goods){
                            $prints[$print_ids[$i]]['goods_ids'][] = isset($flash_sale_goods[$v['goods_id']])?$flash_sale_goods[$v['goods_id']]:$v['goods_id'];
                        }else if($pin_tuan_goods){
                            $prints[$print_ids[$i]]['goods_ids'][] = isset($pin_tuan_goods[$v['goods_id']])?$pin_tuan_goods[$v['goods_id']]:$v['goods_id'];
                        }else{
                            $prints[$print_ids[$i]]['goods_ids'][] = $v['goods_id'];
                        }
                    }
                }
            }
        }
        return compact('count','list','prints');
    }
    public static function up($data,$where){
        $data['edit_time']=date('Y-m-d H:i:s');
        $res=self::update($data,$where);
        return $res;
    }
}

